package com.aiyi.education.admin.service.impl;


import com.aiyi.education.admin.service.IKsAdminPlanService;
import com.aiyi.education.entity.admin.KsAdminPlan;
import com.aiyi.education.mapper.admin.KsAdminPlanMapper;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.education.base.PublicResultConstant;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

/**
 * @author renyong
 * @email sxgyry@163.com
 * @date
 */
@Service
public class KsAdminPlanServiceImpl extends ServiceImpl<KsAdminPlanMapper, KsAdminPlan> implements IKsAdminPlanService {

    @Autowired
    KsAdminPlanMapper ksAdminPlanMapper;

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return ksAdminPlanMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insertSelective(KsAdminPlan record) {
        return ksAdminPlanMapper.insertSelective(record);
    }

    @Override
    public KsAdminPlan selectByPrimaryKey(Integer id) {
        return ksAdminPlanMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(KsAdminPlan record) {
        return ksAdminPlanMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public Map<String, Object> addKsAdminPlan(Map<String, Object> map, Object userObject, KsAdminPlan record) {
        if (StringUtils.isEmpty(record.getUserName())||StringUtils.isEmpty(record.getContent())){
            map.put("result",400);
            map.put("msg",PublicResultConstant.PARAM_ERROR);
            return map;
        }
        this.insertSelective(record);
        map.put("result",200);
        map.put("msg",record);
        return map;
    }

    @Override
    public Map<String, Object> updateKsAdminPlan(Map<String, Object> map, Object userObject, KsAdminPlan record) {
        if (record.getId()==null){
            map.put("result",400);
            map.put("msg",PublicResultConstant.PARAM_ERROR);
            return map;
        }
        this.updateByPrimaryKeySelective(record);
        map.put("result",200);
        map.put("msg",record);
        return map;
    }

    @Override
    public Map<String, Object> deleteKsAdminPlan(Map<String, Object> map, Object userObject, KsAdminPlan record) {
        if (record.getId()==null){
            map.put("result",400);
            map.put("msg",PublicResultConstant.PARAM_ERROR);
            return map;
        }
        this.deleteByPrimaryKey(record.getId());
        map.put("result",200);
        map.put("msg",PublicResultConstant.SUCCEED);
        return map;
    }

    @Override
    public Map<String, Object> searchKsAdminPlan(Map<String, Object> map, Object userObject, Page<KsAdminPlan> pageModel, JSONObject requestJson) {

        QueryWrapper wrapper = new QueryWrapper();
        if (StringUtils.isNotEmpty(requestJson.getString("userName"))){
            wrapper.like("user_name",requestJson.getString("userName"));
        }
        if (StringUtils.isNotEmpty(requestJson.getString("startTime"))){
            SimpleDateFormat startSdf = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
            wrapper.ge("add_date",startSdf.format(requestJson.getString("startTime")));
        }
        if (StringUtils.isNotEmpty(requestJson.getString("endTime"))){
            SimpleDateFormat endSdf = new SimpleDateFormat("yyyy-MM-dd 23:59:59");
            wrapper.ge("add_date",endSdf.format(requestJson.getString("endTime")));
        }
        wrapper.orderByDesc("addDate");
        List<KsAdminPlan> planList = ksAdminPlanMapper.selectByWrapper(pageModel,wrapper);
        map.put("result",200);
        map.put("msg",pageModel.setRecords(planList));
        return null;
    }
}
